<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/7/13
 * Time: 15:25
 */

namespace app\api\controller;

use app\common\lib\Aes;
use app\common\lib\exception\ApiException;
use app\common\lib\IAuth;
use app\common\lib\Time;
use think\Cache;
use think\Controller;
/**
 * API模块 公共的控制器
 * @package app\api\controller
 */
class Common extends Controller
{

    public $page = 1;
    public $size = 2;
    public $start = 0;


    /**
     * 定义header头
     * @var
     */
    public $headers='';
    /**
     *初始化的方法
     */
    public function _initialize()
    {
        //$this->checkRequestAuth();
        $this->testAes();
    }

    /**
     * 校验每次app请求的数据是否合法
     */
    public function checkRequestAuth()
    {
        //首先,需要获取header头里的数据
        $headers = request()->header();
        //halt($headers);
        //sign 加密需要 客户端工程师 , 解密需要服务端工程师

        //1. header body 里的其他数据可以仿照sign加密方法进行加密   sign只是单独拿出来将它进行加密了
        //2. 加密方式多变

        //基础参数校验
        if(empty($headers['sign']))
        {
            throw new ApiException('sign不存在',400);
        }
        //这里header里面参数是app_type时获取不到,不知道为什么
        if(!in_array($headers['type'],config('app.apptypes')))
        {
            throw new ApiException('app_type不合法',400);
        }

        if(!IAuth::checkSignPass($headers))
        {
            throw new ApiException('授权码sign验签失败',401);
        }

        //假如说黑客在sign失效的10秒内破解了怎么办呢?这时我们可以设置sign的唯一性
        //1.文件  2.mysql  3.redis
        Cache::set($headers['sign'],1,config('app_sign_cache_time'));

        //将头部的信息赋值
        $this->headers = $headers;
    }

    public function testAes()
    {
        //这个是跟前端约定好的要加密的数据
        $data = [
            'did'=> '12345dg',
            'version'=>1,
            'time'=>Time::get13TimeStamp()
        ];
        //halt($data);

        //生成sign加密串
        $str = IAuth::setSign($data);
        //echo $str;exit;  //col9j6cqegAKiiey3IrXWk4KfRT35tI7zVf2fJzbdLYIVnKDb7Rin03dOqY2qLWP


        //$str = 'sRCvj52mZ8G+u2OdHYwmysvczmCw+RrAYWiEaXFI/5A=';  //这个是用encrypt方法加密后的字符串,就是上面的那个$data加密后的
       // echo (new Aes())->decrypt($str);exit;
       // echo IAuth::setSign($data);exit;
        /*//$str = 'id=1&username=lbj&age=66';
        //echo (new Aes())->encrypt($str);exit;

        $aes_str = 'h1FqJN4oRbBicQ6p8KeecoUUWEtmlVBQcsj9G7SYYAE=';
        echo (new Aes())->decrypt($aes_str);exit;*/
    }

    /**
     * 获取处理后的新闻数据
     * @param array $news
     * @return array
     */
    protected function getDealNews($news = [])
    {
        if(empty($news))
        {
            return [];
        }
        $cats = config('cat.lists');
        foreach ($news as $key=>$new)
        {
            $news[$key]['catname'] = $cats[$new['catid']]?$cats[$new['catid']]:'-';
        }
        return $news;
    }

    /**
     * 获取 page start 和 size
     * @param $data
     */
    public function getPageAndSize($data)
    {
        $this->page = !empty($data['page'])?$data['page']:1;
        $this->size = !empty($data['size'])?$data['size']:config('paginate.list_rows');
        $this->start = ($this->page - 1) * $this->size;
    }
}